package org.springframework.samples.petclinic.web;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.validation.PetValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ajax.json.AjaxContext;

/**
 * JavaBean Form controller that is used to edit an existing <code>Pet</code>.
 *
 * @author Juergen Hoeller
 * @author Ken Krebs
 */
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm extends PetAndVisit{

	@Autowired
	public EditPetForm(Clinic clinic) {
		super(clinic);
	}

	@ModelAttribute("types")
	public Collection<PetType> populatePetTypes() {
		return this.getClinic().getPetTypes();
	}

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields(new String[] {"id"});
    }

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(@RequestParam("petId") int petId, Model model) {
		Pet pet = this.getClinic().loadPet(petId);
		model.addAttribute("pet", pet);
		
		if(isAjaxRequest()){
			makePopup("petForm", "addOrUpdatePetForm");
		}		
		
		return "petForm";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, 
			SessionStatus status, Model model) {
		new PetValidator().validate(pet, result);
		if (result.hasErrors()) {
			if(isAjaxRequest()){
				makePopup("petForm", "addOrUpdatePetForm");
			}	
			return "petForm";
		}
		else {
			this.getClinic().storePet(pet);
			status.setComplete();
			if(isAjaxRequest()){
				preparePartial(findIndex(pet), pet, true);
			}
			return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
		}
	}
	
}
